package cn.fuxi2022.accounts.message.service.impl;

import cn.fuxi2022.accounts.message.service.WeChatMessageService;
import cn.fuxi2022.accounts.properties.ConstantPropertiesUtil;
import cn.fuxi2022.core.exception.SmartGroupException;
import cn.fuxi2022.core.utils.SHA1;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class WeChatMessageServiceImpl implements WeChatMessageService {
    @Override
    public boolean verifyToken(String signature, String timestamp, String nonce) {
        String[] str = new String[]{token, timestamp, nonce};
        //排序
        Arrays.sort(str);
        //拼接字符串
        StringBuffer buffer = new StringBuffer();
        for (String s : str) {
            buffer.append(s);
        }
        //进行sha1加密
        String temp = SHA1.encode(buffer.toString());
        //与微信提供的signature进行匹对
        return signature.equals(temp);
    }

    @Autowired
    private WxMpService wxMpService;


    @Override
    public void pushPayMessage(String openId, String name, String orderNo, String time, int type) {
        // 1.获取要发送的用户 + 发送的消息模板
        // 2.创建模板消息对象（链式）
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage
                .builder()
                .toUser(openId)
                .templateId(ConstantPropertiesUtil.ORDERP_ROGRESS_NOTIFICATION)
                // 小程序上线后，放开，点击模板将跳转至小程序
                // .miniProgram(new WxMpTemplateMessage.MiniProgram(WeChatAppletsProperties.APP_ID, WeChatAppletsProperties.GROUP_PAGE_PATH, false))
                .build();

        // 3.配置模板消息
        // 创建WxMpTemplateData模板数据（设置的参数名，参数值，显示的颜色）
        if (type == 0) {
            // 当前订单未支付，发送支付提醒
            templateMessage.addData(new WxMpTemplateData("keyword3", WAIT, "#272727"));
            // 设置标题
            templateMessage.addData(new WxMpTemplateData("first", ALERT_FIRST, "#cc0000"));
            // 设置备注
            templateMessage.addData(new WxMpTemplateData("remark", ALERT_REMARK, "#173177"));
        } else if (type == 1) {
            templateMessage.addData(new WxMpTemplateData("keyword3", SUCCESS, "#272727"));
            // 订单已经支付
            templateMessage.addData(new WxMpTemplateData("first", SUCCESS_FIRST, "#cc0000"));
            templateMessage.addData(new WxMpTemplateData("remark", SUCCESS_REMARK, "#173177"));
        } else if (type == 2 || type == 3) {
            templateMessage.addData(new WxMpTemplateData("keyword3", CLOSE, "#272727"));
            // 订单已经关闭
            templateMessage.addData(new WxMpTemplateData("first", FAIL_FIRST, "#cc0000"));
            templateMessage.addData(new WxMpTemplateData("remark", FAIL_REMARK, "#173177"));
        }

        // 设置参数
        templateMessage.addData(new WxMpTemplateData("keyword1", orderNo, "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword2", name, "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword4", time, "#272727"));

        // 5.发送模板消息，获取响应
        try {
            // 返回当前的消息ID
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new SmartGroupException("发送消息失败，请检查消息参数是否正确");
        }
    }

    @Override
    public void pushRegister(String openId, int type, String name, String time) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage
                .builder()
                .toUser(openId)
                .templateId(ConstantPropertiesUtil.REGISTER_NOTICE)
                // 小程序上线后，放开，点击模板将跳转至小程序
                // .miniProgram(new WxMpTemplateMessage.MiniProgram(WeChatAppletsProperties.APP_ID, WeChatAppletsProperties.GROUP_PAGE_PATH, false))
                .build();
        if (type == 0) {
            // 签到成功
            templateMessage.addData(new WxMpTemplateData("first", REGISTER_SUCCESS_FIRST, "#cc0000"));
            templateMessage.addData(new WxMpTemplateData("remark", REGISTER_SUCCESS_REMARK, "#173177"));
        } else if (type == 1) {
            // 签到失败
            templateMessage.addData(new WxMpTemplateData("first", REGISTER_FAIL_FIRST, "#cc0000"));
            templateMessage.addData(new WxMpTemplateData("remark", REGISTER_FAIL_REMARK, "#173177"));
        }

        templateMessage.addData(new WxMpTemplateData("keyword1", name, "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword2", time, "#272727"));

        try {
            // 返回当前的消息ID
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new SmartGroupException("发送消息失败，请检查消息参数是否正确");
        }
    }
}